import { createRouter, createWebHashHistory, NavigationGuardNext, RouteLocationNormalized, RouteRecordRaw } from 'vue-router'




const user = {
  name: 'user',
  path: '/user',
  meta: { title: '会员管理', name: '会员管理' },
  component: () => import('@/views/home/children/user/User.vue')
}

const job = {
  name: 'job',
  path: '/job',
  meta: { title: '兼职管理', name: '兼职管理' },
  component: () => import('@/views/home/children/job/Job.vue')
}

const company = {
  name: 'company',
  path: '/company',
  meta: { title: '企业管理', name: '企业管理' },
  component: () => import('@/views/home/children/company/Company.vue')
}

const adv = {
  name: 'adv',
  path: '/adv',
  meta: { title: '广告管理', name: '广告管理' },
  component: () => import('@/views/home/children/adv/Adv.vue')
}

const contact = {
  name: 'contact',
  path: '/contact',
  meta: { title: '联系方式', name: '联系方式' },
  component: () => import('@/views/home/children/contact/Contact.vue')
}

const admin = {
  name: 'admin',
  path: '/admin',
  meta: { title: '用户管理', name: '用户管理' },
  component: () => import('@/views/home/children/admin/Admin.vue')
}

const jobmanage = {
  name: 'jobmanage',
  path: '/jobmanage',
  meta: { title: '渠道广告', name: '渠道广告' },
  component: () => import('@/views/home/children/jobmanage/Jobmanage.vue')
}



/* 匹配router */
const match: any = {
  user, job,
  company, adv, contact,
  admin, jobmanage
};


/* 动态路由 */
export const DynamicRouting = (): RouteRecordRaw[] => {
  const childrenRoutes: RouteRecordRaw[] = []
  const routerdata = JSON.parse(localStorage.getItem('router') as string);
  if (routerdata) {
    routerdata.forEach((item: any) => {
      childrenRoutes.push(match[item.name]);
    });
  };
  return childrenRoutes;
}



function InitRouterList(): RouteRecordRaw {
  return {
    path: '/',
    name: 'Home',
    component: () => import('@/views/home/Home.vue'),
    children: [
      {
        name: 'index',
        path: '/index',
        meta: { title: '数据汇总', name: '数据汇总' },
        component: () => import('@/views/home/children/index/Index.vue')
      },
      ...DynamicRouting()
    ]
  }
}


const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: '/index'
  },
  {
    path: '/login',
    name: 'login',
    meta: { title: '登录', name: '登录' },
    component: () => import('@/views/login/Login.vue')
  },
  {
    path: '/404',
    name: '404',
    meta: { title: '404', name: '404' },
    component: () => import('@/views/404/404.vue'),
  },
  InitRouterList()
]


const router = createRouter({
  history: createWebHashHistory(),
  routes
})



export const addRoute = () => {
  router.addRoute(InitRouterList())
}


/* 路由守卫 */
router.beforeEach((to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
  document.title = (window as any).title as string
  if (to.matched.length !== 0) {
    if (to.path !== '/login') {
      if (!localStorage.getItem('token')) {
        next({ path: "/login" });
      } else {
        addRoute()
        next()
      }
    } else {
      next()
    };
  } else {
    next({ path: "/404" });
  }
});




export default router
